<?php

App::uses('Crud', 'Model');

class AppliedAnswer extends Crud
{
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;
    
    const IS_OK = 1;
    
    public function getForQuestionExam( $questionId ) {
        $filters = array(
            'conditions' => array(
                'status' => self::STATUS_ENABLED,
                'applied_question_id' => $questionId
            )
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }
    
    
    public function getForQuestionExamView( $questionId ) {
        $filters = array(
            'fields' => array(
                'id',
                'description',
                'image'
            ),
            'conditions' => array(
                'status' => self::STATUS_ENABLED,
                'applied_question_id' => $questionId
            )
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }
    
    public function getAnswersOk( $questionId )
    {
        $filters = array(
            'fields' => array(
                'id'
            ),
            'conditions' => array(
                'status' => self::STATUS_ENABLED,
                'applied_question_id' => $questionId,
                'is_ok' => self::IS_OK
            )
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }
    
    public function getIdsOkBy( $questionId )
    {
        $dataCount = array(
            'fields' => array(
                'id'
            ),
            'conditions' => array(
                'applied_question_id' => $questionId,
                'is_ok' => self::IS_OK
            )
        );
        $records = $this->find('all',$dataCount);
        $answeredQuestions = $this->flatArray($records);
        
        $ids = array();
        foreach( $answeredQuestions as $key => $aq )
        {
            array_push($ids, $aq['id']);
        }
        return $ids; 
    }
    
}